import { IPSModelObject } from '../../../ipsmodel-object';
import { IPSModelService } from '../../../ipsmodel-service';

/**
 * 模型服务缓存帮助类
 *
 * @export
 * @class ModelServiceStore
 */
export class ModelServiceStore {
  /**
   * 忽略类型正则
   *
   * @author chitanda
   * @date 2021-04-10 11:04:41
   * @protected
   */
  protected modelCacheIgnoreReg = /^(app\.logic\.IPSAppUILogicRefView)$/;

  /**
   * 模型服务缓存
   *
   * @type {Map<string, IPSModelService>}
   */
  readonly modelService: Map<string, IPSModelService> = new Map();

  /**
   * 模型对象换粗
   *
   * @type {Map<string, IPSModelObject>}
   */
  readonly modelObject: Map<string, IPSModelObject> = new Map();

  /**
   * 设置模型对象缓存
   *
   * @author chitanda
   * @date 2022-08-09 10:08:09
   * @param {string} cls 模型类型
   * @param {string} strPath 模型路径，相对路径
   * @param {IPSModelObject} modelObject 模型对象实例
   */
  setModelCache(cls: string, strPath: string, modelObject: IPSModelObject): void {
    if (this.cacheIgnore(cls) || cls.indexOf('control.') !== -1) {
      return;
    }
    this.modelObject.set(strPath, modelObject);
  }

  /**
   * 忽略缓存的类型
   *
   * @author chitanda
   * @date 2021-04-10 10:04:34
   * @protected
   * @param {string} cls
   * @return {*}  {boolean}
   */
  protected cacheIgnore(cls: string): boolean {
    if (this.modelCacheIgnoreReg.test(cls)) {
      return true;
    }
    return false;
  }

  /**
   * 获取模型路径
   *
   * @protected
   * @param {IModel} model
   * @return {*}  {string}
   */
  protected getPath(model: IModel): string {
    return model.path || model.dynaModelFilePath;
  }

  /**
   * 清理模型缓存
   *
   * @author chitanda
   * @date 2021-05-20 00:05:57
   */
  clear(): void {
    this.modelObject.clear();
  }

  /**
   * 删除单个模型缓存
   *
   * @author chitanda
   * @date 2022-08-09 14:08:03
   * @param {string} strPath
   * @return {*}  {boolean}
   */
  delete(strPath: string): boolean {
    return this.modelObject.delete(strPath);
  }
}
